GROUPBYSUM
Merges objects in an array that share a specified property, summing numerical values.
Description
Given an array of objects and a property name, merges objects that share the given property. Only numerical entries are merged. Other entry types are discarded. If a given object is not a JavaScript object or does not have the key, it will be disregarded.
Syntax
GROUPBYSUM(array, key)
Arguments
array
: An array of JavaScript objects.key
: A string containing the property to be merged by.
Example
GROUPBYSUM(A1:A5, "category") → Returns an array of objects merged by the "category" property, with numerical values summed.
GROUPBYSUM([{category: "A", value: 10}, {category: "B", value: 20}, {category: "A", value: 5}], "category") → Returns [{category: "A", value: 15}, {category: "B", value: 20}].
Usage Notes:
- Only numerical properties (excluding the grouping key) are summed.
- Non-object or null entries in the array are ignored.
- Objects missing the specified key are ignored.
- The function returns an array of objects, with each object representing a unique group based on the specified key.